import { createSlice, PayloadAction } from '@reduxjs/toolkit';

export interface BusinessPlan {
  id: string;
  opportunityId: string;
  title: string;
  content: string;
  followupFrequency: number; // 跟进频率（天）
  followupMethods: string[]; // 跟进方式
  resourceAllocation: {
    manpower: number;
    budget: number;
    time: number;
  };
  riskAssessment: {
    level: 'low' | 'medium' | 'high';
    factors: string[];
    mitigation: string[];
  };
  actionPlan: Array<{
    id: string;
    task: string;
    deadline: string;
    assignee: string;
    status: 'pending' | 'in_progress' | 'completed';
  }>;
  compliance: {
    status: 'compliant' | 'warning' | 'violation';
    issues: string[];
  };
  status: 'draft' | 'pending_approval' | 'approved' | 'rejected';
  createdAt: string;
  updatedAt: string;
  createdBy: string;
  approvedBy?: string;
  approvedAt?: string;
  version: number;
}

export interface BusinessPlanState {
  plans: BusinessPlan[];
  currentPlan: BusinessPlan | null;
  generating: boolean;
  loading: boolean;
  error: string | null;
  templates: Array<{
    id: string;
    name: string;
    description: string;
    content: string;
  }>;
}

const initialState: BusinessPlanState = {
  plans: [],
  currentPlan: null,
  generating: false,
  loading: false,
  error: null,
  templates: [],
};

const businessPlanSlice = createSlice({
  name: 'businessPlan',
  initialState,
  reducers: {
    setLoading: (state, action: PayloadAction<boolean>) => {
      state.loading = action.payload;
    },
    setGenerating: (state, action: PayloadAction<boolean>) => {
      state.generating = action.payload;
    },
    setError: (state, action: PayloadAction<string | null>) => {
      state.error = action.payload;
    },
    setPlans: (state, action: PayloadAction<BusinessPlan[]>) => {
      state.plans = action.payload;
    },
    addPlan: (state, action: PayloadAction<BusinessPlan>) => {
      state.plans.unshift(action.payload);
    },
    updatePlan: (state, action: PayloadAction<BusinessPlan>) => {
      const index = state.plans.findIndex(plan => plan.id === action.payload.id);
      if (index !== -1) {
        state.plans[index] = action.payload;
      }
      if (state.currentPlan && state.currentPlan.id === action.payload.id) {
        state.currentPlan = action.payload;
      }
    },
    deletePlan: (state, action: PayloadAction<string>) => {
      state.plans = state.plans.filter(plan => plan.id !== action.payload);
      if (state.currentPlan && state.currentPlan.id === action.payload) {
        state.currentPlan = null;
      }
    },
    setCurrentPlan: (state, action: PayloadAction<BusinessPlan | null>) => {
      state.currentPlan = action.payload;
    },
    updateActionStatus: (state, action: PayloadAction<{ planId: string; actionId: string; status: 'pending' | 'in_progress' | 'completed' }>) => {
      const plan = state.plans.find(p => p.id === action.payload.planId);
      if (plan) {
        const actionItem = plan.actionPlan.find(a => a.id === action.payload.actionId);
        if (actionItem) {
          actionItem.status = action.payload.status;
          plan.updatedAt = new Date().toISOString();
        }
      }
      if (state.currentPlan && state.currentPlan.id === action.payload.planId) {
        const currentAction = state.currentPlan.actionPlan.find(a => a.id === action.payload.actionId);
        if (currentAction) {
          currentAction.status = action.payload.status;
          state.currentPlan.updatedAt = new Date().toISOString();
        }
      }
    },
    setTemplates: (state, action: PayloadAction<BusinessPlanState['templates']>) => {
      state.templates = action.payload;
    },
  },
});

export const {
  setLoading,
  setGenerating,
  setError,
  setPlans,
  addPlan,
  updatePlan,
  deletePlan,
  setCurrentPlan,
  updateActionStatus,
  setTemplates,
} = businessPlanSlice.actions;

export default businessPlanSlice.reducer;
